/*ReservationCMD.java
 * 
 * this command creates a new reservation and adds it to the
 * collection of reservations for a given tour offering inside
 * of the tour offering collection in TourApp
 *
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */


import java.util.*;



public class ReservationCMD extends Cmd
{
   /* variables */
   public String travelerName;
   public String offeringName;
   public String status;
   public String partnerName;
   public String city;
   public String state;
   public int fitnessLvl;
   public int gender = 0;  //0 being female, 1 being male
   public int age = 0;
   
   /* constructor */
   public ReservationCMD() 
   { 
      super(); 
   }
   
   //command format:
   // token 0          1                2                     3                 4         5      6      7      8
   // reservation <personname><tourofferingname><solo|single|partnername><fitnessrating><city><state><gender><age>
   // gender only required for "solo" reservation 
   
   public void exec()
   {
      if( token.length >= 7 )
      {
         
         travelerName = token[1];
         offeringName = token[2];
         
         if( token[3].compareTo( "solo" ) == 0 )
         {
            if( token.length >= 9 )
            {
               status = "solo";
               partnerName = "none";
               if( token[7].toLowerCase().compareTo("m") == 0 )
               {
                  gender = 1;
               }
               else if( token[7].toLowerCase().compareTo("f") == 0 )
               {
                  gender = 0;
               }
               else{
                  System.err.print( "Invalid gender field\n" );
                  return;
               }
               int a;
               try{ a =Integer.parseInt( token[ 8 ] ); }
               catch( NumberFormatException e )
               {
                  System.out.printf( " bad int %s\n", token[8], e.getMessage( ));
                  return;
               }
               age = a;
            }
            else
            {
               System.err.print( "Not enough tokens on reservation command line for a \"solo\"\n" );
               return;
            }
         }
         else if( token[3].compareTo("single") == 0 )
         {
            status = "single";
            partnerName = "none";
         }
         else
         {
            status = "double";
            partnerName = token[3];
         }
         
         int f;
         try{ f =Integer.parseInt( token[ 4 ] ); }
         catch( NumberFormatException e )
         {
            System.out.printf( " bad int %s\n", token[2], e.getMessage( ));
            return;
         }
         
         city = token[5];
         state = token[6];
         
         fitnessLvl = f;
         TourOff tourOff = tourApp.getTourOffering( offeringName );
         
         if( tourOff == null )
         {
            System.err.printf( "Tour offering %s does not exist!\n",
                              offeringName );
            return;
         }
         
         if( f < tourOff.tour().fitnessLevel() )
         {
            System.err.printf( "Fitness level too low!\n" );
            return;
         }
         
         //-----------Reservation Creation------------------
         Reservation newRes = new Reservation( travelerName, offeringName, status,
                                              partnerName, fitnessLvl, city, state );
         //set gender if new reservation is a solo
         if( newRes.status().compareTo( "solo" ) == 0){
            newRes.setGender( gender );
            newRes.setAge( age );
         }
         //-------------------------------------------------
         if(  tourOff.reservations().find( travelerName ) )
         {
            System.err.printf( "Reservation under this name: %s already exists!\n",
                              travelerName );
            return;
         }
         if( tourOff.addReservation( newRes ) == false )
         {
            System.err.printf( "Tour Offering " + offeringName +
                              " is full! Reservation for " + 
                              travelerName + " not scheduled\n" );
         }
         
      }
      else
      {
         System.err.print( "Not enough tokens on reservation line!\n" );
         return;
      }
      
   }
   //end
}
